package com.software.library_conquer;

/**
 * Created by CwT on 15/6/7.
 */

import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class WebClient {

    public static String get(final String url, HashMap<String, String> arg){
        URL htmlUrl = null;
        Log.d("cc", "url: "+ url);
        InputStream inStream = null;
        try {
            htmlUrl = new URL(url);
            URLConnection connection = htmlUrl.openConnection();
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            Iterator iter = arg.entrySet().iterator();
            while (iter.hasNext()){
                Map.Entry entry = (Map.Entry)iter.next();
                httpConnection.setRequestProperty((String)entry.getKey(), (String)entry.getValue());
            }
            int responseCode = httpConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inStream = httpConnection.getInputStream();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return inputStreamToString(inStream);
    }

    public static String put(final String url, HashMap<String, String> arg, String content){
        URL htmlUrl = null;
        Log.d("cc", "url: "+ url);
        InputStream inStream = null;
        try {
            htmlUrl = new URL(url);
            URLConnection connection = htmlUrl.openConnection();
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            Iterator iter = arg.entrySet().iterator();
            while (iter.hasNext()){
                Map.Entry entry = (Map.Entry)iter.next();
                httpConnection.setRequestProperty((String)entry.getKey(), (String)entry.getValue());
            }

            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("PUT");
            OutputStreamWriter out = new OutputStreamWriter(httpConnection.getOutputStream());
            out.write(content);
            out.close();

            int responseCode = httpConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inStream = httpConnection.getInputStream();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return inputStreamToString(inStream);
    }

    public static String post(final String url, HashMap<String, String> arg, String content){
        URL htmlUrl = null;
        Log.d("cc", "url: "+ url);
        InputStream inStream = null;
        try {
            htmlUrl = new URL(url);
            URLConnection connection = htmlUrl.openConnection();
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            Iterator iter = arg.entrySet().iterator();
            while (iter.hasNext()){
                Map.Entry entry = (Map.Entry)iter.next();
                httpConnection.setRequestProperty((String)entry.getKey(), (String)entry.getValue());
            }

            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
            OutputStreamWriter out = new OutputStreamWriter(httpConnection.getOutputStream());
            out.write(content);
            out.close();

            int responseCode = httpConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inStream = httpConnection.getInputStream();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return inputStreamToString(inStream);
    }

    public static String inputStreamToString(InputStream is) {
        try {
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            int ch;
            while ((ch = is.read()) != -1) {
                bytestream.write(ch);
            }
            String data = bytestream.toString();
            bytestream.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


}

